﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.IO;
using System.Collections.Generic;

namespace MyQuestApp.utils
{
    public class ImageConvertor
    {

        public static byte[] BitmapImageToBinary(BitmapImage bitmapImage)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                WriteableBitmap btmMap = new WriteableBitmap
                    (bitmapImage.PixelWidth, bitmapImage.PixelHeight);

                // write an image into the stream
                Extensions.SaveJpeg(btmMap, ms,
                    bitmapImage.PixelWidth, bitmapImage.PixelHeight, 0, 100);

                return ms.ToArray();
            }

        }

        public static BitmapImage BinaryToBitmapImage(byte[] rawImageBytes)
        {
            BitmapImage imageSource = null;
            //try
            //{
                 using (MemoryStream stream = new MemoryStream(rawImageBytes))
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    BitmapImage b = new BitmapImage();
                    b.SetSource(stream);
                    imageSource = b;
                }
           // }
            //catch (System.Exception ex)
            //{
            //}
            return imageSource;
        }

        public static List<BitmapImage> ByteImageListToBitmapImageList(List<byte[]> byteImages)
        {
            List<BitmapImage> authPics=new List<BitmapImage>();

            foreach (byte[] iterator in byteImages)
            {
                authPics.Add(BinaryToBitmapImage(iterator));
            }

            return authPics;
        }

    }
}
